package org.emaait.app.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.emaait.app.exception.ActionNotFoundException;
import org.emaait.app.model.User;
import org.emaait.app.service.IUserService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class UserAction implements Controller {
	private static final String ACTION = "action";
	private static final String ACTION_REGISTER = "register";
	private static final String REGISTER_USERID = "userid";
	private static final String REGISTER_EMAIL = "email";
	private static final String REGISTER_PASSWORD = "password";
	private static final String REGISTER_SU = "register_su";

	public ModelAndView handleRequest(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		String action = req.getParameter(ACTION);
		String method=req.getParameter("m");
		if (action!=null&&action.equals(ACTION_REGISTER)) {
			if (method!=null&&method.equals("create")) {
				User user = new User();
				user.setEmail(req.getParameter(REGISTER_EMAIL));
				user.setPassword(req.getParameter(REGISTER_PASSWORD));
				user.setUserId(req.getParameter(REGISTER_USERID));
				userService.create(user);
				ModelAndView mv = new ModelAndView(REGISTER_SU);
				mv.addObject("user", user);
				mv.addObject("message", "注册成功!");
				return mv;
			}else{
				return new ModelAndView(REGISTER_SU);
			}
		}else{
			throw new ActionNotFoundException("404");
		}
	}

	private IUserService userService;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

}
